package org.phoenixframework.api.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.phoenixframework.api.rbac.mapper.SysAuthorityMapper;
import org.phoenixframework.api.rbac.mapper.SysRoleMapper;
import org.phoenixframework.api.rbac.mapper.SysUserMapper;
import org.phoenixframework.api.rbac.service.ISysUserService;
import org.phoenixframework.common.entity.SysAuthority;
import org.phoenixframework.common.entity.SysRole;
import org.phoenixframework.common.entity.SysUser;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author doublelife
 * @since 2021-05-08
 */
@AllArgsConstructor
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    private final SysUserMapper sysUserMapper;

    private final SysRoleMapper sysRoleMapper;

    private final SysAuthorityMapper sysAuthorityMapper;

    private final RedisTemplate redisTemplate;

    @Override
    public SysUser findByUsername(String username) {
        return sysUserMapper.findByUsername(username);
    }

    @Override
    public Map<String, Object> findSysUserInfo(String username) {
        SysUser sysUser = sysUserMapper.findByUsername(username);
        List<SysRole> sysRoles = sysRoleMapper.listBySysUserId(sysUser.getId());
        List<String> roles = sysRoles.stream().map(SysRole::getName).collect(Collectors.toList());
        if(roles.size() == 0) {
            //前端框架必须返回一个角色，否则报错，如果没有角色，返回一个空角色
            roles.add("");
        }
        List<String> authorities;
        if (1 == sysUser.getIsSuper()) {
            LambdaQueryWrapper<SysAuthority> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SysAuthority::getType, 2);
            authorities = sysAuthorityMapper.selectList(lqw).stream().map(SysAuthority::getAuthorityValue).collect(Collectors.toList());
        } else {
            authorities = sysAuthorityMapper.findAuthorityValueByUserId(sysUser.getId());
        }
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        authorities.forEach(item -> grantedAuthorities.add(new SimpleGrantedAuthority(item)));
        redisTemplate.opsForValue().set(username, grantedAuthorities);

        Map<String, Object> result = new HashMap<>(8);
        result.put("name", username);
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        result.put("roles", roles);
        result.put("permissionValueList", authorities);
        return result;
    }

    @Override
    public Map<String, Object> findRolesByUserId(Long userId) {
        // 所有角色
        List<SysRole> sysRoles = sysRoleMapper.selectList(new LambdaQueryWrapper<>());
        // 用户已分配的角色
        List<SysRole> assignRoles = sysUserMapper.findRolesByUserId(userId);
        Map<String, Object> result = new HashMap<>(8);
        result.put("assignRoles", assignRoles);
        result.put("allRolesList", sysRoles);
        return result;
    }
}
